package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.mapper.ProductSpecMapper;
import com.spzx.product.service.IProductSpecService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.ProductSpecVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品规格 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2024-11-12
 */
@Service
public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, ProductSpec> implements IProductSpecService {

    @Override
    public List<ProductSpec> selectProductSpecList() {
        return baseMapper.selectProductSpecList();
    }

    @Override
    public List<ProductSpecVo> selectProductSpecListByCategoryId(Long categoryId) {

        //v1
       /* List<ProductSpec> productSpecList = baseMapper.selectProductSpecListByCategoryId(categoryId);
        List<ProductSpecVo> productSpecVoList = productSpecList.stream().map(productSpec -> {
            ProductSpecVo productSpecVo = new ProductSpecVo();
            BeanUtils.copyProperties(productSpec, productSpecVo);
            return productSpecVo;
        }).toList();
        return productSpecVoList;*/

        //v2
        //return baseMapper.selectProductSpecListByCategoryId(categoryId);

        //v3
        LambdaQueryWrapper<ProductSpec> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSpec::getCategoryId, categoryId);
        List<ProductSpec> productSpecList = baseMapper.selectList(queryWrapper);
        List<ProductSpecVo> productSpecVoList = productSpecList.stream().map(productSpec -> {
            ProductSpecVo productSpecVo = new ProductSpecVo();
            BeanUtils.copyProperties(productSpec, productSpecVo);
            return productSpecVo;
        }).toList();
        return productSpecVoList;
    }
}
